local skill = fk.CreateSkill{
  name = "tea__shiwan",
}
---@type TrigSkelSpec<TrigFunc>
local shiwan = {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name) and player == target and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local isFirst = true
    while isFirst or room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#tea__shiwan-invoke"}) do
      player:drawCards(1, skill.name)
      if isFirst then
        isFirst = false
      end
      local num = 0
      room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDraw and move.to == player and move.toArea == Player.Hand then
            num = num + #move.moveInfo
          end
        end
        return false
      end, Player.HistoryPhase)
      local targets = table.filter(room.alive_players, function(p)
        return table.every(room:getOtherPlayers(p), function(p1)
          return #p:getCardIds("h") >= #p1:getCardIds("h")
        end)
      end)
      if #targets > 0 then
        for _, p in ipairs(targets) do
          room:askToDiscard(p, {
            min_num = num,
            max_num = 999,
            skill_name = skill.name,
            include_equip = true,
            cancelable = false,
            prompt = "#tea__shiwan-discard:::" .. tostring(num),
          })
        end
        if table.contains(targets, player) then break end
      end
    end
  end,
}
skill:addEffect(fk.EventPhaseStart, shiwan)
return skill